package ${package.Controller};

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.book.common.result.Failed;
import com.book.common.result.RestResponse;
import com.book.common.result.Success;
import com.book.common.result.PageInfo;
import com.book.common.api.helper.TransformHelper;
import com.book.common.api.entity.CurrentUser;
import ${package.Entity}.${entity};
import ${cfg.dtoPath}.${entity}Dto;
import ${cfg.voPath}.${entity}Vo;
import ${package.Service}.${table.serviceName};
import com.book.common.api.core.RESTfulMethod;
import com.book.modules.core.controller.BaseController;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import com.book.modules.annotation.InjectionCurrentUser;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * Description：
 * @Author： ${author}
 * @CreateDate： ${date}
 * @Email： ${cfg.email}
 * Version: ${cfg.version}
 */
@Slf4j
@Validated
@RestController
@Api(value = "${table.comment!}信息")
@RequestMapping(value = "${r"${adminPath}"}/${table.entityPath}")
public class ${entity}Controller extends BaseController{

    @Autowired
    private ${table.serviceName} ${table.entityPath}Service;

    /**
     * 根据ID获取${table.comment!}信息
     *
     * @param id
     * @param currentUser
     * @return
     */
    @GetMapping(value = "{id}", consumes = "application/json")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "根据id获取${table.comment!}信息", httpMethod = RESTfulMethod.GET, response = Success.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "${table.comment!}ID", required = true)
    })
    public RestResponse get(@NotBlank(message = "{required}") @PathVariable String id, @ApiIgnore CurrentUser currentUser) {
        try {
            if (StringUtils.isEmpty(id)) {
                return new Failed("BOOK:COMMON:10003", i18nHelper.get("BOOK:COMMON:10003"));
            }
            return new Success(this.${table.entityPath}Service.getById(id), i18nHelper.get("BOOK:COMMON:10004"));
        } catch (Exception e) {
            log.error("查询${table.comment!}失败",e);
            return new Failed("BOOK:COMMON:10005", i18nHelper.get("BOOK:COMMON:10005")+e.getMessage());
        }
    }

    /**
     * 查询${table.comment!}信息
     *
     * @param ${table.entityPath}Dto
     * @param currentUser
     * @return
     */
    @PostMapping(value = "list")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "查询${table.comment!}信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse list(@RequestBody ${entity}Dto ${table.entityPath}Dto, @ApiIgnore CurrentUser currentUser) {
        try {
            ${entity} ${table.entityPath} = TransformHelper.transform(${table.entityPath}Dto,${entity}.class);
            List<${entity}> ${table.entityPath}List=this.${table.entityPath}Service.findAllList(${table.entityPath});
            return new Success(TransformHelper.transformList(${table.entityPath}List,${entity}Vo.class), i18nHelper.get("BOOK:COMMON:10004"));
        } catch (Exception e) {
            log.error("查询${table.comment!}失败", e);
            return new Failed("BOOK:COMMON:10006", i18nHelper.get("BOOK:COMMON:10006")+e.getMessage());
        }
    }

    /**
     * 查询${table.comment!}信息（分页）
     *
     * @param ${table.entityPath}Dto
     * @param currentUser
     * @return
     */
    @PostMapping(value = "page/list")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "查询${table.comment!}分页信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse pageList(@RequestBody ${entity}Dto ${table.entityPath}Dto, @ApiIgnore CurrentUser currentUser) {
        try {
            ${entity} ${table.entityPath} = TransformHelper.transform(${table.entityPath}Dto,${entity}.class);
            PageInfo<${entity}> page = new PageInfo(${table.entityPath}Dto.getPageNo(),${table.entityPath}Dto.getPageSize());
            page = this.${table.entityPath}Service.findPageList(${table.entityPath},page);
            return new Success(TransformHelper.transformPage(page,${entity}Vo.class), i18nHelper.get("BOOK:COMMON:10004"));
        } catch (Exception e) {
            log.error("分页查询${table.comment!}失败", e);
            return new Failed("BOOK:COMMON:10007", i18nHelper.get("BOOK:COMMON:10007")+e.getMessage());
        }
    }

    /**
     * 新增${table.comment!}信息
     *
     * @param ${table.entityPath}Dto
     * @param currentUser
     * @return
     */
    @PostMapping(value = "save")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "保存${table.comment!}信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse save(@Valid @RequestBody ${entity}Dto ${table.entityPath}Dto, @ApiIgnore CurrentUser currentUser) {
        try {
            this.${table.entityPath}Service.save(TransformHelper.transform(${table.entityPath}Dto,${entity}.class));
            return new Success("", i18nHelper.get("BOOK:COMMON:10008"));
        } catch (Exception e) {
            log.error("新增${table.comment!}失败", e);
            return new Failed("BOOK:COMMON:10009", i18nHelper.get("BOOK:COMMON:10009")+e.getMessage());
        }
    }

    /**
     * 修改${table.comment!}信息
     *
     * @param ${table.entityPath}Dto
     * @param currentUser
     * @return
     */
    @PutMapping(value = "update")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "修改${table.comment!}信息", httpMethod = RESTfulMethod.PUT, response = Success.class)
    public RestResponse update(@Valid @RequestBody ${entity}Dto ${table.entityPath}Dto, @ApiIgnore CurrentUser currentUser) {
        try {
            if (${table.entityPath}Dto==null || ${table.entityPath}Dto.getId() == null) {
                return new Failed("BOOK:COMMON:10010", i18nHelper.get("BOOK:COMMON:10010"));
            }
            this.${table.entityPath}Service.update(Wrappers.update(TransformHelper.transform(${table.entityPath}Dto,${entity}.class)));
            return new Success(i18nHelper.get("BOOK:COMMON:10011"));
        } catch (Exception e) {
            log.error("修改${table.comment!}失败", e);
            return new Failed("BOOK:COMMON:10012", i18nHelper.get("BOOK:COMMON:10012")+e.getMessage());
        }
    }

    /**
     * 删除${table.comment!}信息
     *
     * @param id
     * @param currentUser
     * @return
     */
    @DeleteMapping(value = "delete/{id}")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "根据ID删除${table.comment!}信息", httpMethod = RESTfulMethod.DELETE, response = Success.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "${table.comment!}ID", required = true)
    })
    public RestResponse delete(@NotBlank(message = "{required}") @PathVariable String id, @ApiIgnore CurrentUser currentUser) {
        try {
            this.${table.entityPath}Service.removeByIds(Lists.newArrayList(id.split(StringPool.COMMA)));
            return new Success("", i18nHelper.get("BOOK:COMMON:10013"));
        } catch (Exception e) {
            log.error("删除${table.comment!}失败", e);
            return new Failed("BOOK:COMMON:10014", i18nHelper.get("BOOK:COMMON:10014")+e.getMessage());
        }
    }

    /**
     * 导出${table.comment!}模板信息
     *
     * @param currentUser
     * @return
     */
    @PostMapping(value = "export/template")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "导出${table.comment!}模板信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse exportTemplate(@ApiIgnore CurrentUser currentUser) {
        try {
            super.excelHelper.exportExcel("${table.comment!}",${entity}Vo.class);
            return new Success("", i18nHelper.get("BOOK:COMMON:10015"));
        } catch (Exception e) {
            log.error("导出${table.comment!}模板信息失败", e);
            return new Failed("BOOK:COMMON:10016", i18nHelper.get("BOOK:COMMON:10016")+e.getMessage());
        }
    }

    /**
     * 导出${table.comment!}信息
     *
     * @param ${table.entityPath}Dto
     * @param currentUser
     * @return
     */
    @PostMapping(value = "export/excel")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "导出${table.comment!}信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse exportExcel(@RequestBody ${entity}Dto ${table.entityPath}Dto, @ApiIgnore CurrentUser currentUser){
        try {
            List<${entity}> ${table.entityPath}List = this.${table.entityPath}Service.findAllList(TransformHelper.transform(${table.entityPath}Dto,${entity}.class));
            super.excelHelper.exportExcel("${table.comment!}",${entity}Vo.class,TransformHelper.transformList(${table.entityPath}List,${entity}Vo.class));
            return new Success("", i18nHelper.get("BOOK:COMMON:10024"));
        } catch (Exception e) {
            log.error("导出${table.comment!}信息失败", e);
            return new Failed("BOOK:COMMON:10025", i18nHelper.get("BOOK:COMMON:10025")+e.getMessage());
        }
    }

    /**
     * 导入${table.comment!}信息
     *
     * @param filePath
     * @param currentUser
     * @return
     */
    @PostMapping("import/excel")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "导入${table.comment!}信息", httpMethod = RESTfulMethod.POST, response = Success.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "filePath", value = "保存文件路径", required = true)
    })
    public RestResponse importExcel(@RequestParam(value = "filePath") String filePath, @ApiIgnore CurrentUser currentUser){
        try {
            List<${entity}Vo> ${table.entityPath}VoList = super.excelHelper.importExcel(filePath,${entity}Vo.class);
            if(CollectionUtils.isEmpty(${table.entityPath}VoList)){
                return new Failed("BOOK:COMMON:10026", i18nHelper.get("BOOK:COMMON:10026"));
            }
            this.${table.entityPath}Service.saveBatch(TransformHelper.transformList(${table.entityPath}VoList,${entity}.class));
            return new Success("", i18nHelper.get("BOOK:COMMON:10027"));
        } catch (Exception e) {
            log.error("导入${table.comment!}信息失败", e);
            return new Failed("BOOK:COMMON:10028", i18nHelper.get("BOOK:COMMON:10028")+e.getMessage());
        }
    }
}
